class UserController < ApplicationController
  require 'digest/sha1'
  def login
  end
  def login_try
    if params[:username] and params[:password]
      params[:user] = Hash.new
      params[:user][:username] = params[:username]
      params[:user][:password] = params[:password]
    end
    if params[:user][:username] and params[:user][:password]
      if user = User.find(:first, :conditions => ["username = ? and password = ?",params[:user][:username],Digest::SHA1.hexdigest(params[:user][:password])])
        session[:user_id] = user.id
        redirect_to home_url
      else
        redirect_to :back
      end
    else
      redirect_to :back
    end
  end
  def verify_email
    if params[:user][:email]
      Notifier::deliver_verify_email(params[:user][:email])
      flash[:success] = "Check your email."
    end
    redirect_to :back
  end
  def register
  end
  def register_try
    if params[:user][:username] and params[:user][:password] and params[:email]
      email = params[:email].split("_")
      if email[0].crypt("kewl")==email[1] and !User.find(:first, :conditions => ["email = ? or username = ?",email[0],params[:user][:username]])
        user = User.new(:email => email[0], :password => Digest::SHA1.hexdigest(params[:user][:password]), :firstname => params[:user][:firstname], :lastname => params[:user][:lastname], :username => params[:user][:username])
        user.save
        session[:user_id] = user.id
        redirect_to home_url
      else
        redirect_to :back
      end
    else
      redirect_to :back
    end
  end
  def logout
    session[:user_id] = nil
    redirect_to :action => :login
  end
end 
